/*
 * $Id: DBQuote.java,v 1.1 2007/08/29 07:43:52 xun Exp $
 */

package projectx.db;

import java.text.SimpleDateFormat;
import java.util.Date;

import projectx.util.DateUtil;
import projectx.util.StringUtil;

public class DBQuote {

	// --------------------------------------------------------------------------
	// S T A T I C M E N B E R S
	//
	// --------------------------------------------------------------------------

	public static final String NULL = "NULL";

	public static final String SYS_DATE = "CURRENT DATE";

	public static final String SYS_TIME = "CURRENT TIME";

	public static final String SYS_TIMESTAMP = "CURRENT TIMESTAMP";

	private static final SimpleDateFormat FORMATTER = new SimpleDateFormat(
			"yyyy-MM-dd HH:mm:ss");

	// --------------------------------------------------------------------------
	// C O N S T R U C T O R S
	//
	// --------------------------------------------------------------------------
	private DBQuote() {
		super();
	}

	// --------------------------------------------------------------------------
	// S T A T I C M E T H O D S
	//
	// --------------------------------------------------------------------------

	public static String quoteChar(String sData) {
		// Empty String instead of NULL
		if (sData == null) {
			return " NULL ";
		}

		StringBuffer buffer = new StringBuffer(" '");
		String tempData = prepareStringForDB(sData);
		buffer.append(tempData);
		buffer.append("' ");
		return buffer.toString();
	}

	private static String prepareStringForDB(String sData) {
		StringBuffer buffer = new StringBuffer();
		String tempData = StringUtil.replaceAll(sData, "'", "${QUOTE}");
		tempData = StringUtil.replaceAll(tempData, "${QUOTE}", "''");
		buffer.append(tempData.trim());
		return buffer.toString();
	}

	public static String quoteTimeStamp(String sTM, boolean isBegin) {
		StringBuffer buffer = new StringBuffer(" ");
		String temp = dateFormat(sTM, '-');
		if (temp == null) {
			buffer.append(NULL);
		} else {
			buffer.append("'");
			buffer.append(temp);
			if (temp.length() == 10) {
				if (isBegin) {
					buffer.append(" 00:00:00");
				} else {
					buffer.append(" 23:59:59");
				}
			}
			buffer.append("' ");
		}
		return buffer.toString();
	}

	public static String quoteDate(String sDate) {
		StringBuffer buffer = new StringBuffer(" ");

		if (StringUtil.isBlank(sDate)) {
			buffer.append(NULL);
		} else if (isDefaultDateValue(sDate)) {
			buffer.append(sDate);
		} else {
			buffer.append(quoteChar(dateFormat(sDate, '-')));
		}
		return buffer.toString();
	}

	public static String quoteDate(Date dData) {
		StringBuffer buffer = new StringBuffer(" ");
		if (dData == null) {
			buffer.append(NULL);
		}
		buffer.append("TIMESTAMP('");
		buffer.append(FORMATTER.format(dData));
		buffer.append("') ");
		return buffer.toString();
	}

	public static String quoteChars(String[] sDatas) {
		StringBuffer buffer = new StringBuffer();
		if (sDatas != null && sDatas.length > 0) {
			for (int i = 0; i < sDatas.length; i++) {
				if (StringUtil.isBlank(sDatas[i])) {
					continue;
				}

				if (buffer.length() > 0) {
					buffer.append(",");
				}
				buffer.append(quoteChar(sDatas[i]));
			}
		}
		if (buffer.length() > 0) {
			buffer.insert(0, " ( ");
			buffer.append(" ) ");
		}
		return buffer.toString();
	}

	public static String quoteInts(int[] sDatas) {
		StringBuffer buffer = new StringBuffer(" (");
		if (sDatas != null && sDatas.length > 0) {
			for (int i = 0; i <= sDatas.length; i++) {
				if (i > 0) {
					buffer.append(",");
				}
				buffer.append(sDatas[i]);
			}
		}
		buffer.append(") ");
		return buffer.toString();
	}

	public static String quoteChars(double[] datas) {
		StringBuffer buffer = new StringBuffer(" (");
		if (datas != null && datas.length > 0) {
			for (int i = 0; i <= datas.length; i++) {
				if (i > 0) {
					buffer.append(",");
				}
				buffer.append(datas[i]);
			}
		}
		buffer.append(") ");
		return buffer.toString();
	}

	public static String quoteNumber(String number) {
		if (number == null || number.trim().length() == 0) {
			return "0";
		}
		return number;
	}

	private static String dateFormat(String sDate, char symbol) {
		if (sDate == null || !(sDate.length() == 10 || sDate.length() == 8)) {
			return null;
		} else if (isDefaultDateValue(sDate)) {
			return sDate;
		} else if (sDate.length() == 8 && symbol == '-') {
			return DateUtil.formatDate(sDate, DateUtil.DATE_PATTERN,
					DateUtil.DATE_VIEW_PATTERN);
		} else {
			String newDate = sDate.replace('.', symbol);
			newDate = newDate.replace('-', symbol);
			newDate = newDate.replace('/', symbol);
			return newDate;
		}
	}

	private static boolean isDefaultDateValue(String sDate) {
		if (StringUtil.isBlank(sDate)) {
			return false;
		}

		if (sDate.equals(SYS_DATE)) {
			return true;
		}
		if (sDate.equals(SYS_TIME)) {
			return true;
		}
		if (sDate.equals(SYS_TIMESTAMP)) {
			return true;
		}

		return false;
	}

	public static String quoteGraphic(String sData) {
		if (sData == null) {
			return NULL;
		}

		return " GRAPHIC(" + quoteChar(sData) + ") ";
	}
}
